package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.enums.ResultEnums;
import com.qf.exception.AirException;
import com.qf.pojo.AirQualityIndex;
import com.qf.pojo.District;
import com.qf.service.AirService;
import com.qf.service.DistrictService;
import com.qf.vo.ResultVo;
import com.qf.vo.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/air")
public class AirController {
    @Autowired
    AirService airService;
    @Autowired
    DistrictService districtService;

    @GetMapping("/list")
    public String list(String name, Model model, @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "size",defaultValue = "5")Integer size){

        PageInfo<AirQualityIndex> pageInfo = airService.pageInfo(name, page, size);
        List<District> allDistrict = districtService.districtList();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("allDistrict", allDistrict);

        return "index";
    }

//    跳转到添加页面

    @GetMapping("/add-ui")
    public String addUI(Model model){
        List<District> districts = districtService.districtList();
        model.addAttribute("districts", districts);
        return "add";

    }

    @PostMapping("/add")
    @ResponseBody
    public ResultVo add(@Valid AirQualityIndex airQualityIndex, BindingResult bindingResult, Integer districtid){
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            throw new AirException(ResultEnums.PARAM_ERROR.getCode(),message);
        }

        airQualityIndex.setDistrictid(districtid);

        Integer count =airService.save(airQualityIndex);
        if (count!=1){
            throw new AirException(ResultEnums.SAVE_ERROE);
        }

        return ResultVoUtil.success();
    }

//    跳转到维护页面
    @GetMapping("/update-ui")
    public String updateUI(Model model,Integer id){

        List<District> districts = districtService.districtList();
        AirQualityIndex airQualityIndex = airService.findById(id);

        model.addAttribute("airQualityIndex", airQualityIndex);
        model.addAttribute("districts", districts);
        model.addAttribute("id",id );
        return "update";
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultVo update(@Valid AirQualityIndex airQualityIndex, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            throw new AirException(ResultEnums.PARAM_ERROR.getCode(),message);
        }

       Integer count= airService.update(airQualityIndex);
        if (count!=1){
            throw new AirException(ResultEnums.UPDATE_ERROR);
        }
        return ResultVoUtil.success();
    }

//    删除操作
    @GetMapping("/delete")
    @ResponseBody
    public ResultVo delete(Integer id){
        Integer count= airService.deleteById(id);
        if (count!=1){
            throw new AirException(ResultEnums.DELETE_ERROR);
        }
        return ResultVoUtil.success();
    }



}
